﻿<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<jsp:include page="../inc/meta.jsp"></jsp:include>
<jsp:include page="../inc/easyui.jsp"></jsp:include>

<script type="text/javascript" src="<%=request.getContextPath()%>/Content/jquery-easyui-datagridview/datagrid-detailview.js">
</script>

</head>
<body class="easyui-layout" fit="true">

	<!-- 			title="权限组列表（所属系统：${systemText}" -->
	<div id="content" region="center" style="overflow: hidden">
		<table id="permissionList" fit="true" border="false"
		title="权限组列表"
			url="<%=request.getContextPath()%>/permission/GetPermissionGroups"
			idField="id">
			<thead>
				<tr>
					<th field="ck" checkbox="true"></th>
					<th field="name" width="80">权限组名</th>
					<th field="description" width="200" align="center">描述</th>
					<th field="createDate" width="120" align="center">创建日期</th>
				</tr>
			</thead>
		</table>
	</div>
	<script type="text/javascript">
		var permissionGrid;
		$(function() {
			permissionGrid = $('#permissionList')
					.datagrid(
							{
								view : detailview,
								fitColumns : true,
								pageSize: 10,
				                pagination: true,
				                pageList: [10, 20, 30, 40, 50, 60, 70, 80, 90, 100],
								detailFormatter : function(index, row) {
									return '<div><table id="ddv-' + index + '"></table></div>';
								},
								toolbar : [ {
									text : '创建',
									iconCls : 'icon-add',
									handler : function() {
										CreatePermissionGroup();
									}
								}, '-', {
									text : '编辑',
									iconCls : 'icon-edit',
									handler : function() {
										EditPermissionGroup();
									}
								}, '-', {
									text : '删除',
									iconCls : 'icon-remove',
									handler : function() {
										DeletePermissionGroup();
									}
								}, '-', {
									text : '取消选中',
									iconCls : 'icon-undo',
									handler : function() {
										permissionGrid.datagrid('unselectAll');
									}
								} ],
								onExpandRow : function(index, row) {
									var subgridId = 'ddv-' + index;
									$('#' + subgridId).datagrid({
														url : '<%=request.getContextPath()%>/permission/getPermissionItems?permissionGroupId='
																+ row.id,
														//title: '权限项列表[所属组：'+row.Name+']',
														fitColumns : true,
														rownumbers : true,
														loadMsg : '正在载入权限项，请稍后...',
														height : 'auto',
														toolbar : [
																{
																	text : '创建',
																	iconCls : 'icon-add',
																	handler : function() {
																		CreatePermissionItem(
																				subgridId,
																				row.id);
																	}
																}

																],
														columns : [ [
																{
																	field : 'ck',
																	checkbox : true
																},
																{
																	field : 'code',
																	title : '代码',
																	width : 150
																},
																{
																	field : 'displayName',
																	title : '名称',
																	width : 150
																},
																{
																	field : 'operation',
																	title : '操作选项',
																	align : 'center',
																	width : 120,
																	formatter : function(
																			value,
																			row,
																			index) {
																		// var editStr = "<a name=\"operButton\"  class=\"easyui-linkbutton\" iconcls=\"icon-edit\"  plain=\"true\" href=\"javascript:EditPermissionItem('" + row.Id + "');\">编辑</a>";
																		var editStr = "<a class=\"l-btn l-btn-plain\" href=\"javascript:EditPermissionItem('"
																				+ subgridId
																				+ "','"
																				+ row.id
																				+ "')\" style=\"float: left;\"><span class=\"l-btn-left\"><span class=\"l-btn-text icon-edit\" style=\"padding-left: 60px;\">编辑</span></span></a>";
																		// var delStr = "<a name=\"operButton\" class=\"easyui-linkbutton\"  iconcls=\"icon-remove\"  plain=\"true\" href=\"javascript:DeletePermissionItem('" + row.Id + "');\"> 删除</a>"
																		var delStr = "<a class=\"l-btn l-btn-plain\" href=\"javascript:DeletePermissionItem('"
																				+ subgridId
																				+ "','"
																				+ row.id
																				+ "')\" style=\"float: left;\"><span class=\"l-btn-left\"><span class=\"l-btn-text icon-remove\" style=\"padding-left: 80px;\">删除</span></span></a>";
																		return editStr
																				+ '<div class="datagrid-btn-separator"></div>'
																				+ delStr;
																	}
																} ] ],
														onResize : function() {
															$('#permissionList')
																	.datagrid(
																			'fixDetailRowHeight',
																			index);
														},
														onLoadSuccess : function() {
															setTimeout(
																	function() {
																		$(
																				'#permissionList')
																				.datagrid(
																						'fixDetailRowHeight',
																						index);
																	}, 0);

														}
													});
									$('#permissionList').datagrid(
											'fixDetailRowHeight', index);
								}
							});
		});

		function reloadDatagrid(datagridId) {
		$('#' + datagridId).datagrid("unselectAll");
			$('#' + datagridId).datagrid('reload');
			
			var arr = datagridId.split('-');
			permissionGrid.datagrid('fixDetailRowHeight', arr[1]);
		}
		function reloadMasterGrid() {
			permissionGrid.datagrid("unselectAll");
			permissionGrid.datagrid('reload');
		}

		function CreatePermissionItem(datagridId, permisssionGroupId) {
			var url = '<%=request.getContextPath()%>/permission/toCreatePermissionItem?permissionGroupId=' + permisssionGroupId;
// 			url = $.format(url+ '?permissionGroupId={0}', permisssionGroupId);
			//showMyWindow('创建权限项', 'icon-add', url, 620, 400, true, false, false);
			var p = top.sy.dialog({
						title : '创建权限项',
						iconCls : 'icon-add',
						href : url,
						width : 450,
						height : 350,
						modal : true,
						minimizable : false,
						maximizable : false,
						cache : false,
						buttons : [
								{
									text : '确定',
									iconCls : 'icon-save',
									handler : function() {
										var f = p.find('form');
										f.form('submit',{
										  url : '<%=request.getContextPath()%>/permission/createPermissionItem',
										  onsubmit : function() {
											   var flag = $(this).form('validate');
												if (flag)
												{
													top.showProcess(true,'温馨提示','正在提交数据...');
												}
												return flag;
											},
										 success : function(data) {
											    top.showProcess(false);
												var map = $.parseJSON(data);
												if (map.success) {
													top.showMsg('提示',map.msg);
													reloadDatagrid(datagridId);
													p.dialog('close');
												} else {
													top.alertMsg('错误',map.msg+ "\n"+ map.obj==null?"":map.obj);
												}

											},
											onLoadError : function() {
												top.showProcess(false);
												top.$.messager.alert('温馨提示','由于网络或服务器太忙，提交失败，请重试！');
											}
									});

									}
								}, {
									text : '取消',
									iconCls : 'icon-cancel',
									handler : function() {
										p.dialog('close');
									}
								} ]
					});
			
			
		}
		function ViewPermissionItem(datagridId, permissionGroupId) {

		}
	
		function OrderPermissionItem(datagridId, permissionGroupId) {
            var url = '/taskpro/permission/toEditPermissionItem.do?userId='+userId;
			top.showMyWindow('编辑个人角色', 'icon-edit', url, 830, 480, true, false, true);
		}
		
		function EditPermissionItem(datagridId, permissionItemId) {
			var url = '<%=request.getContextPath()%>/permission/toEditPermissionItem?permissionItemId=' + permissionItemId;
// 			url = $.format(url+ '?permissionItemId={0}&systemId={1}',permissionItemId,systemId);
			//showMyWindow('编辑权限项', 'icon-edit', url, 630, 400, true, false,
					//false);
			var p = top.sy.dialog({
			title: '编辑权限项',
			iconCls: 'icon-edit',
			href: url,
			width: 450,
			height: 350,
			modal: true,
			minimizable: false,
			maximizable: false,
			cache: false,
			buttons: [{
				text: '确定',
				iconCls: 'icon-save',
				handler: function() {
					var f = p.find('form');
					f.form('submit', {
						url: '<%=request.getContextPath()%>/permission/editPermissionItem',
						onsubmit: function() {
							var flag = $(this).form('validate');
							if (flag) {
								top.showProcess(true, '温馨提示', '正在提交数据...');
							}
							return flag;
						},
						success: function(data) {
							top.showProcess(false);
							var map = $.parseJSON(data);
							if (map.success) {
								top.showMsg('提示', map.msg);
								reloadDatagrid(datagridId);
								p.dialog('close');
							} else {
								top.alertMsg('错误', map.msg + "\n" + map.obj == null ? "" : map.obj);
							}

						},
						onLoadError: function() {
							top.showProcess(false);
							top.$.messager.alert('温馨提示', '由于网络或服务器太忙，提交失败，请重试！');
						}
					});

				}
			}, {
				text: '取消',
				iconCls: 'icon-cancel',
				handler: function() {
					p.dialog('close');
				}
			}]
		});
	}
		function DeletePermissionItem(datagridId, permissionItemId) {
			top.$.messager.confirm(
								'提示',
								'确定删除吗？',
								function(r) {
									if (r) {
										$.ajax({
											url: '<%=request.getContextPath()%>/permission/deletePermissionItem?permissionItemId=' + permissionItemId,
											cache: false,
											dataType: "json",
											success: function(data) {
												if (data.success) {
													reloadDatagrid(datagridId);
													top.showMsg('提示',
													data.msg);
												} else {
													top.alertMsg(
														'警告',
													data.msg + "\n" + data.obj);
												}
											}
										});
									}
								});

		}
		function ViewPermissionGroup() {
		}
		//创建权限组
		function CreatePermissionGroup() {
			var url = '<%=request.getContextPath()%>/permission/toCreatePermissionGroup';
			var p = top.sy.dialog({
						title : '创建权限组',
						iconCls : 'icon-add',
						href : url,
						width : 450,
						height : 350,
						modal : true,
						minimizable : false,
						maximizable : false,
						cache : false,
						buttons : [
								{
									text : '确定',
									iconCls : 'icon-save',
									handler : function() {
										var f = p.find('form');
										f.form('submit', {
											url: '<%=request.getContextPath()%>/permission/CreatePermissionGroup',
											onsubmit: function() {
												var flag = $(this).form('validate');
												if (flag) {
													top.showProcess(
													true,
														'温馨提示',
														'正在提交数据...');
												}
												return flag;
											},
											success: function(data) {
												top.showProcess(false);
												var map = $.parseJSON(data);
												if (map.success) {
													top.showMsg(
														'提示',
													map.msg);
													reloadMasterGrid();
													p.dialog('close');
												} else {
													top.alertMsg(
														'错误',
													map.msg + "\n" + map.obj);
												}

											},
											onLoadError: function() {
												top.showProcess(false);
												top.$.messager.alert(
													'温馨提示',
													'由于网络或服务器太忙，提交失败，请重试！');
											}
										});

									}
								}, {
									text : '取消',
									iconCls : 'icon-cancel',
									handler : function() {
										p.dialog('close');
									}
								} ]
					});

		}

		//编辑权限组
		function EditPermissionGroup() {
			var rows = permissionGrid.datagrid('getSelections');
			if (rows.length > 0) {
				if (rows.length == 1) {
					//弹出编辑框
					var url = '<%=request.getContextPath()%>/permission/toEditPermissionGroup?permissionGroupId='
							+ rows[0].id;
					var p = top.sy
							.dialog({
								title : '编辑权限组',
								iconCls : 'icon-edit',
								href : url,
								width : 450,
								height : 350,
								modal : true,
								minimizable : false,
								maximizable : false,
								cache : false,
								buttons : [
										{
											text : '确定',
											iconCls : 'icon-save',
											handler : function() {
												var f = p.find('form');
												f.form('submit',{
																	url : '<%=request.getContextPath()%>/permission/EditPermissionGroup',
																	onsubmit : function() {
																		var flag = $(this).form('validate');
																		if (flag) {
																			top.showProcess(true,'温馨提示','正在提交数据...');
																		}
																		return flag;
																	},
																	success : function(data) {
																		top.showProcess(false);
																		var map = $.parseJSON(data);
																		if (map.success) {
																			top.showMsg('提示',map.msg);
																			reloadMasterGrid();
																			p.dialog('close');
																		} else {
																			top.alertMsg('错误',map.msg+ "\n"+ map.obj==null?"":map.obj);
																		}

																	},
																	onLoadError : function() {
																		top.showProcess(false);
																		top.$.messager.alert('温馨提示','由于网络或服务器太忙，提交失败，请重试！');
																	}
																});

											}
										}, {
											text : '取消',
											iconCls : 'icon-cancel',
											handler : function() {
												p.dialog('close');
											}
										} ]
							});

				} else {
					var names = [];
					for ( var i = 0; i < rows.length; i++) {
						names.push(rows[i].name);
					}
					top.showMsg("提示", '只能选择一个权限组进行编辑！您已经选择了【' + names.join(',')
							+ '】' + rows.length + '个权限组');
				}
			} else {
				top.showMsg("提示", "请选择要编辑的权限组！");
			}

		}
		//删除权限组
		function DeletePermissionGroup() {
			var ids = [];
			var rows = permissionGrid.datagrid('getSelections');
			if (rows.length > 0) {
				var names = [];
				for ( var i = 0; i < rows.length; i++) {
					names.push(rows[i].name);
				}
				top.$.messager.confirm(
								'提示',
								'确定要删除权限组【' + names.join(',') + '】吗？',
								function(r) {
									if (r) {
										for ( var i = 0; i < rows.length; i++) {
											ids.push(rows[i].id);
										}
										$.ajax({
													url : '<%=request.getContextPath()%>/permission/DeletePermissionGroup',
													data : {
														permissionGroupIds : ids.join(',')
													},
													cache : false,
													dataType : "json",
													success : function(data) {
														if (data.success) {
															reloadMasterGrid();
															top.showMsg('提示',
																	data.msg);
														} else {
															top.alertMsg(
																			'警告',
																			data.msg
																					+ "\n"
																					+ data.obj);
														}
													}
												});
									}
								});
			} else {
				top.showMsg("提示", "请选择要删除的权限组！");
			}
		}

		
	</script>
</body>
</html>

